package com.zd.ticketing.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zd.common.bean.model.HardwareInfo;
import com.zd.common.bean.model.TicketInfo;
import com.zd.common.common.utils.HttpRequestUtil;
import com.zd.ticketing.bean.vo.ScenicSpotDetailVo;
import com.zd.ticketing.service.IScenicSpotService;
import com.zd.ticketing.service.ITicketInfoService;
import com.zd.ticketing.service.ITicketOrderService;

@Controller
@RequestMapping("pc/automatic")
public class PCAutomaticTicketController {

	@Autowired
	private IScenicSpotService scenicSpotService;

	@Autowired
	private ITicketInfoService ticketInfoService;

	@Autowired
	private ITicketOrderService ticketOrderService;
	

	@RequestMapping("/index")
	public ModelAndView automaticIndex() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("PC/menuIndex");
		return mv;
	}

	@ResponseBody
	@RequestMapping("getScenicSpot")
	public Map<String, Object> getScenicSpot(HardwareInfo hardRes) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		HardwareInfo hardwareInfo = scenicSpotService.getHardwareInfo(hardRes);
		resultMap.put("hardwareInfo", hardwareInfo);
		return resultMap;
	}

	@RequestMapping("/toVoteTicket")
	public ModelAndView toVoteTicket(HardwareInfo hardRes) throws Exception {// 购票
		ModelAndView mv = new ModelAndView();
		HardwareInfo hardwareInfo = scenicSpotService.getHardwareInfo(hardRes);
		ScenicSpotDetailVo scenicSpotDetailVo = scenicSpotService.getScenicSpotDetail(hardwareInfo.getAppId());
		List<TicketInfo> ticketInfoList = ticketInfoService.getPCScenicSpotTicketList(scenicSpotDetailVo.getId());
		mv.addObject("ticketInfoList", ticketInfoList);
		mv.setViewName("PC/ticketPurchase");
		return mv;
	}

	@RequestMapping("/placeAnOrder")
	@ResponseBody
	public Map<String, String> placeAnOrder(String userIdInfoList, Integer ticketInfoId, String deviceNo,HttpServletRequest request) throws Exception {
		Map<String, String> resultMap = ticketOrderService.buyTiecktsPC(userIdInfoList, ticketInfoId,
				HttpRequestUtil.getIp(request),deviceNo );
		return resultMap;
	}
	
	@RequestMapping("/pollingFindOrder")
	@ResponseBody
	public Map<String, Object> pollingFindOrder(Integer orderId) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap = ticketOrderService.pollingFindOrder(orderId);
		return resultMap;
	}
	
	@RequestMapping("/getCheckCode")
	@ResponseBody
	public Map<String, Object> getCheckCode(String uuid,String deviceNo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", false);
		resultMap.putAll(ticketOrderService.getCheckCode(uuid, deviceNo));
		return resultMap;
	}
	
}
